<?php

namespace app\store\model;

use app\common\model\Coupon as CouponModel;

/**
 * 优惠券模型
 * Class Coupon
 * @package app\store\model
 */
class Coupon extends CouponModel
{
    /**
     * 获取优惠券列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($param =[])
    {
        $where = [];
        if (self::$region['province'] > 0 && self::$region['city'] > 0) {
            $where = ['province_id' => self::$region['province'], 'city_id' => self::$region['city']];
        }
        if (isset($param['city_id']) && $param['city_id'] > 0) {
            $this->where(['city_id' => $param['city_id']]);
        }
        return $this->where('is_delete', '=', 0)
            ->where($where)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }

    /**
     * 添加新记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        $data['wxapp_id'] = self::$wxapp_id;
        if (!isSuper()) {
            $data['province_id'] = self::$region['province'];
            $data['city_id'] = self::$region['city'];
        }
        if ($data['expire_type'] == '20') {
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
        }
        return $this->allowField(true)->save($data);
    }

    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        // $data['province_id'] = self::$region['province'];
        // $data['city_id'] = self::$region['city'];
        if ($data['expire_type'] == '20') {
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
        }
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 删除记录 (软删除)
     * @return bool|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]) !== false;
    }

}
